-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: minor refactorings of packages and namings #113
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the Match
implementation is wrong (could be used wrong):
if len(predicates) = 0
, match will be true which seems to be wrong.
func Match[T any](items []T, predicates ...Predicate[T]) []T {
var filteredPools []T
for _, pool := range items {
match := true
for _, predicate := range predicates {
if !predicate(pool) {
match = false
break
}
}
if match {
filteredPools = append(filteredPools, pool)
}
}
return filteredPools
}
this should work as well
func Match[T any](items []T, predicates ...Predicate[T]) []T {
var filteredPools []T
for _, pool := range items {
match := false
for _, predicate := range predicates {
if predicate(pool) {
match = true
break
}
}
if match {
filteredPools = append(filteredPools, pool)
}
}
return filteredPools
}
negating the Match function does not work, because all predicates need to match. For example: filteredPoolList := filter.Match(poolList.Items,
MatchesFlavor(r.Spec.Flavor),
MatchesImage(r.Spec.ImageName),
MatchesProvider(r.Spec.ProviderName),
MatchesGitHubScope(r.Spec.GitHubScopeRef.Name, r.Spec.GitHubScopeRef.Kind),
) with the initial implemenation, a pool item would be only added to the filtered list, if all predicates return true. Otherwise the item would be added if only one of the predicated matches, for example If the function is called like so filteredPoolList := filter.Match(poolList.Items) all of the items are returned, because there is no predicate provided to test/filter against. |
Some minor refactorings and renamings for better clarity.